package morenoapp.net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import morenoapp.util.MorenoUtil;
import org.apache.pivot.web.PostQuery;
import org.apache.pivot.web.QueryException;

/**
 * Network communication unility methods.
 * @author dmitry.mamonov
 */
public class MorenoNet {
    public static void createIssue(final Throwable throwable) {
        new Thread(new Runnable() {
            public void run() {
                String title = "" + throwable.getMessage();
                String body = MorenoUtil.toStirng(throwable);
                String[] labelKeys = new String[]{
                    "java.runtime.name", //=Java(TM) SE Runtime Environment
                    "java.vm.version", //=11.0-b15
                    "java.vm.vendor", //=Sun Microsystems Inc.
                    "java.vm.name", //=Java HotSpot(TM) Client VM
                    "user.country", //=RU
                    "sun.os.patch.level", //=Service Pack 3
                    "java.runtime.version", //=1.6.0_10-b33
                    "java.awt.graphicsenv", //=sun.awt.Win32GraphicsEnvironment
                    "os.arch", //=x86
                    "os.name", //=Windows XP
                    "sun.jnu.encoding", //=Cp1251
                    "java.class.version", //=50.0
                    "sun.management.compiler", //=HotSpot Client Compiler
                    "os.version", //=5.1
                    "file.encoding", //=UTF-8
                    "java.specification.version", //=1.6
                    "java.vm.specification.version", //=1.0
                    "sun.arch.data.model", //=32
                    "user.language", //=ru
                    "awt.toolkit", //=sun.awt.windows.WToolkit
                    "java.vm.info", //=mixed mode
                    "java.version", //=1.6.0_10
                    "morena.debug", //=true
                };
                List<String> labelsList = new ArrayList<String>();
                labelsList.add("Type-Exception");
                for (String key : labelKeys) {
                    String labelValue = "" + System.getProperty(key);
                    labelValue = labelValue.replace(" ", "-");
                    labelsList.add(key + "-" + labelValue);
                }

                //do post request
                PostQuery post = new PostQuery("morena-backend.appspot.com", "/IssuesProxyServlet");
                post.setValue(body);
                post.getParameters().add("title", title);
                //post.getParameters().add("body", body);
                int index = 0;
                for (String labelValue : labelsList) {
                    index++;
                    post.getParameters().add("label-" + index, labelValue);
                }
                try {
                    post.execute();
                } catch (QueryException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();

    }

    public static void main(String[] args) {
        for (Entry p : System.getProperties().entrySet()) {
            System.out.println("" + p);
        }

        createIssue(new Exception(new Exception(new Exception(new Exception()))));
    }
}
